package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;

import com.cskaoyan.shopping.form.CheckAllItemRequest;
import com.cskaoyan.shopping.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.form.CartRequest;


import java.util.List;


/**
 * @author makabaka
 * @date 2022-12-02 19:51
 */
@RestController
public class ShoppingCartsController {


    @Autowired
    ICartService iCartService;

    /*
     * @Author makabaka
     * @Description //    根据uid和pid删除购物车商品
     * @Date 19:55 2022/12/2
     * @Param []
     * @return
     **/
    @DeleteMapping("/shopping/carts/{uid}/{pid}")
    public ResponseData deleteShoppingByUidAndPid(@PathVariable("uid") Long uid,
                                                  @PathVariable("pid") Long pid) {
        iCartService.deleteCartItem(uid, pid);
        return new ResponseUtil().setData("成功");
    }

    /*
     * @Author makabaka
     * @Description // [uid]  删除购物车中选中的商品
     * @Date  2022/12/3
     * @Param [uid]
     * @return
     **/
    @DeleteMapping("/shopping/items/{uid}")
    public ResponseData deleteShoppingByUid(@PathVariable("uid") Long uid) {
        iCartService.deleteCheckedItem(uid);
        return new ResponseUtil().setData("成功");

    }

    /*
     * @Author makabaka
     * @Description //   全选购物车的商品
     * @Date 9:06 2022/12/3
     * @Param []
     * @return
     **/
    @PutMapping("/shopping/items")
    public ResponseData shoppingItems(@RequestBody CheckAllItemRequest checkAllItemRequest) {
        iCartService.checkAllCartItem(checkAllItemRequest);
        return new ResponseUtil().setData("成功");
    }

    /**
     * @param userInfo 请求头参数，用于获取用户ID（uid）
     * @return 返回封装List<CartProductDto>的json字符串
     * @author wangzhenyu
     */
    @GetMapping("shopping/carts")
    public ResponseData queryCartsList(@RequestHeader("user_info") String userInfo) {
        // 获取登陆对象的userId
        JSONObject jsonObject = JSON.parseObject(userInfo);
        Long uid = jsonObject.getLong("uid");
        List<CartProductDto> cartListById = iCartService.getCartListById(uid);
        return new ResponseUtil().setData(cartListById);
    }


    /**
     * 添加商品到购物车中
     *
     * @param cartRequest {"userId":"62","productId":100053202,"productNum":1}
     * @return
     * @author wangzhenyu
     */
    @PostMapping("shopping/carts")
    public ResponseData addItemToCart(@RequestBody CartRequest cartRequest) {
        iCartService.addToCart(cartRequest);
        return new ResponseUtil<>().setData("成功");
    }

    /**
     * 实现更新购物车数据的功能
     *
     * @param cartRequest {userId: "62", productId: 100053312, productNum: 3, checked: "true"}
     * @return {"success":true,"message":"success","code":200,"result":"成功","timestamp":1587794411224}
     * @author wangzhenyu
     */
    @PutMapping("shopping/carts")
    public ResponseData updateCart(@RequestBody CartRequest cartRequest) {
        iCartService.updateCartNum(cartRequest);
        return new ResponseUtil<>().setData("成功");
    }
}
